package com.jkt.rms.dataservice.impl;

import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;

import com.jkt.rms.business.Rate;
import com.jkt.rms.commons.utils.RMSException;
import com.jkt.rms.dataservice.AbstractDataService;
import com.jkt.rms.dataservice.RateDataService;
import com.jkt.rms.vo.RateVO;

public class RateDataServiceImpl extends AbstractDataService<Rate> implements RateDataService  {

	@Override
	protected Class<Rate> getReferenceClass() {
		return Rate.class;
	}

	public void addRate(RateVO rateVO) throws RMSException{		
		Rate rateBO = new Rate();
		BeanUtils.copyProperties(rateVO, rateBO);
		rateBO.setStartdate(rateVO.getStartdateStr());
		rateBO.setEnddate(rateVO.getEnddateStr());
		rateBO.setStatus('1');
		rateBO.setUpdatedDate(new Date());
		
		Rate rateDB = loadRate(rateBO);
		if(rateDB != null){
			throw new RMSException("rate.already.exist");
		}else{
			save(rateBO);
		}
	}
	
	protected Rate loadRate(Rate rateBO) {
		Criteria criteria = getSession().createCriteria(getReferenceClass());
		Rate rate = (Rate) criteria.add(
				Restrictions.eq(Rate.PROP_VENDOR_CODE, rateBO.getVendorCode())).add(
						Restrictions.eq(Rate.PROP_ROLE_CODE, rateBO.getRoleCode())).add(
								Restrictions.eq(Rate.PROP_LOCATION_CODE, rateBO.getLocationCode()))
				.uniqueResult();
		return rate;
	}
	
}
